<?php
class Tax_config extends Model
{	
	/*
	Determines if a given item_id is an item
	*/
	function exists($item_id)
	{
		$this->db->from('taxs');	
		$this->db->where('tax_id',$item_id);
		$query = $this->db->get();
		return ($query->num_rows()==1);
	}
	
	/*
	Preform a search on items
	*/
	function search($search)
	{
		$this->db->from('taxs');
		$this->db->like('name', $search); 
		$this->db->order_by("name", "asc");				
		return $this->db->get();	
	}
		
	/*
	Returns all the items
	*/
	function get_all()
	{
		$this->db->from('taxs');
		$this->db->order_by("name", "asc");
		return $this->db->get();		
	}
	
	/*
	Returns all the items
	*/
	function get_tax_coponents($item_id)
	{
		$this->db->from('taxs_items');
		$this->db->where('tax_id',$item_id);
		$this->db->order_by("tax_item_id", "asc");
		return $this->db->get();		
	}
	
	/*
	Gets information about a particular item
	*/
	function get_info($item_id)
	{
		$this->db->from('taxs');
		$this->db->where('tax_id',$item_id);
		$query = $this->db->get();
		
		if($query->num_rows()==1)
		{
			return $query->row();
		}
		else
		{
			//Get empty base parent object, as $item_id is NOT an item
			$item_obj=new stdClass();
			
			//Get all the fields from items table
			$fields = $this->db->list_fields('taxs');
			
			foreach ($fields as $field)
			{
				$item_obj->$field='';
			}
			
			return $item_obj;
		}
	}
	
	/*
	Gets information about multiple items
	*/
	function get_multiple_info($item_ids)
	{
		$this->db->from('taxs');
		$this->db->where_in('tax_id',$item_ids);
		$this->db->order_by("name", "asc");
		return $this->db->get();	
	}
	
	/*
	Inserts or updates a item
	*/
	function save(&$item_data,$item_id=false)
	{		
		if (!$item_id or !$this->exists($item_id))
		{
			if($this->db->insert('taxs',$item_data))
			{
				$item_data['tax_id']=$this->db->insert_id();
				return true;
			}
			return false;
		}
		
		$this->db->where('tax_id', $item_id);
		return $this->db->update('taxs',$item_data);		
	}
	
	/*
	Inserts or updates a item
	*/
	function update_tax(&$item_data,$item_id=false)
	{		
		if ($item_id or $this->exists($item_id))
		{
			if($this->db->where('tax_id', $item_id))
			{
				return $this->db->update('taxs',$item_data);
			}
			return false;
		}	
	}
	
	/*
	Inserts order items
	*/
	function save_tax_items($items_data)
	{		
		$this->db->trans_start();
		if($this->db->insert('taxs_items',$items_data))
		{
			$items_data['insert_id'] = $this->db->insert_id();
			$this->db->trans_complete();
			return $items_data['insert_id'];
		}
		return false;	
	}
	
	/*
	Inserts order items
	*/
	function update_tax_items($items_data,$update_id=false)
	{
		if($update_id)
		{
			$this->db->where('tax_item_id', $update_id);
			return $this->db->update('taxs_items',$items_data);
		}	
		return false;
	}
	
	
	/*
	Updates multiple items at once
	*/
	function update_multiple($item_data,$item_ids)
	{
		$this->db->where_in('tax_id',$item_ids);
		return $this->db->update('taxs',$item_data);		
	}
	
	/*
	Deletes one item
	*/
	function delete($item_id)
	{
		return $this->db->delete('taxs', array('tax_id' => $item_id)); 
	}
	
	/*
	Deletes a list of items
	*/
	function delete_list($item_ids)
	{
		$this->db->where_in('tax_id',$item_ids);
		$this->db->delete('taxs_items');
		
		$this->db->where_in('tax_id',$item_ids);
		return $this->db->delete('taxs');		
 	}
	
	function delete_tax_item($item_ids)
	{
		$this->db->where_in('tax_item_id',$item_ids);
		return $this->db->delete('taxs_items');	
 	}
 	
 
////////// MEK ///////////////////////////////////////////////////////////////////////////////////////////////////////
	/*
	Gets tax for edting
	*/
	function get_tax_for_editing($tax_id)
	{
		$final_data = array();
		//Get the info about the order from the order table
		$this->db->from('taxs');
		$this->db->where('tax_id',$tax_id);
		$final_data['query_taxs']  = $this->db->get();
		
		//$this->db->select('order_item_cost,order_quantity,order_item_number,order_number,order_item_id,name,category,brand');
		$this->db->from('taxs_items');
		$this->db->where('tax_id',$tax_id);
		$final_data['query_tax_items'] = $this->db->get();
			
		return $final_data;
	}

}
?>
